/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

#include "stdax.h"

#include "MdSpi.h"
//#pragma warning(disable : 4996)

// USER_API参数
extern CThostFtdcMdApi* pMdApi;

extern char FRONT_ADDR[];		// 前置地址
extern char BROKER_ID[];		// 经纪公司代码
extern char INVESTOR_ID[];		// 投资者代码
extern char PASSWORD[];			// 用户密码
extern char *g_pInstrumentID[]; //合约代码
extern int instrumentNum;

// 连接成功应答  
void CMdSpi::OnFrontConnected()  
{  
    std::cout << "=====建立网络连接成功=====" << std::endl;  
    // 开始登录  
    CThostFtdcReqUserLoginField loginReq;  
    memset(&loginReq, 0, sizeof(loginReq));  
    strcpy(loginReq.BrokerID, BROKER_ID);  
    strcpy(loginReq.UserID, INVESTOR_ID);  
    strcpy(loginReq.Password, PASSWORD);  
    static int requestID = 0; // 请求编号  
    int rt = pMdApi->ReqUserLogin(&loginReq, requestID);  
    if (!rt)  
        std::cout << ">>>>>>发送登录请求成功" << std::endl;  
    else  
        std::cerr << "--->>>发送登录请求失败" << std::endl;  
} 

// 登录应答  
void CMdSpi::OnRspUserLogin(  
        CThostFtdcRspUserLoginField *pRspUserLogin, 
        CThostFtdcRspInfoField *pRspInfo, 
        int nRequestID, 
        bool bIsLast)  
{  
    bool bResult = pRspInfo && (pRspInfo->ErrorID != 0);  
    if (!bResult)  
    {  
        std::cout << "=====账户登录成功=====" << std::endl;  
        std::cout << "交易日： " << pRspUserLogin->TradingDay << std::endl;  
        std::cout << "登录时间： " << pRspUserLogin->LoginTime << std::endl;  
        std::cout << "经纪商： " << pRspUserLogin->BrokerID << std::endl;  
        std::cout << "帐户名： " << pRspUserLogin->UserID << std::endl;  
        // 开始订阅行情  
        int rt = pMdApi->SubscribeMarketData(g_pInstrumentID, instrumentNum);  
        if (!rt)  
            std::cout << ">>>>>>发送订阅行情请求成功" << std::endl;  
        else  
            std::cerr << "--->>>发送订阅行情请求失败" << std::endl;  
    }  
    else  
        std::cerr << "返回错误--->>> ErrorID=" << pRspInfo->ErrorID << ", ErrorMsg=" << pRspInfo->ErrorMsg << std::endl;  
}

// 订阅行情应答  
void CMdSpi::OnRspSubMarketData(  
    CThostFtdcSpecificInstrumentField *pSpecificInstrument,   
    CThostFtdcRspInfoField *pRspInfo,   
    int nRequestID,   
    bool bIsLast)  
{  
    bool bResult = pRspInfo && (pRspInfo->ErrorID != 0);  
    if (!bResult)  
    {  
        std::cout << "=====订阅行情成功=====" << std::endl;  
        std::cout << "合约代码： " << pSpecificInstrument->InstrumentID << std::endl;  
        // 如果需要存入文件或者数据库，在这里创建表头,不同的合约单独存储  
        char filePath[100] = {'\0'};  
        sprintf(filePath, "%s_market_data.csv", pSpecificInstrument->InstrumentID);  

    }  
    else  
        std::cerr << "返回错误--->>> ErrorID=" << pRspInfo->ErrorID << ", ErrorMsg=" << pRspInfo->ErrorMsg << std::endl;  
} 

// 行情详情通知  
void CMdSpi::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)  
{  
    // 打印行情，字段较多，截取部分  
    std::cout << "=====获得深度行情=====" << std::endl;  
    std::cout << "交易日： " << pDepthMarketData->TradingDay << std::endl;  
    std::cout << "交易所代码： " << pDepthMarketData->ExchangeID << std::endl;  
    std::cout << "合约代码： " << pDepthMarketData->InstrumentID << std::endl;  
    std::cout << "合约在交易所的代码： " << pDepthMarketData->ExchangeInstID << std::endl;  
    std::cout << "最新价： " << pDepthMarketData->LastPrice << std::endl;  
    std::cout << "数量： " << pDepthMarketData->Volume << std::endl;  
    // 如果只获取某一个合约行情，可以逐tick地存入文件或数据库  
    char filePath[100] = {'\0'};  
    sprintf(filePath, "%s_market_data.csv", pDepthMarketData->InstrumentID);  


    // 计算实时k线  
    std::string instrumentKey = std::string(pDepthMarketData->InstrumentID);  



    // 取消订阅行情  
    //int rt = g_pMdUserApi->UnSubscribeMarketData(g_pInstrumentID, instrumentNum);  
    //if (!rt)  
    //  std::cout << ">>>>>>发送取消订阅行情请求成功" << std::endl;  
    //else  
    //  std::cerr << "--->>>发送取消订阅行情请求失败" << std::endl;  
}  